package com.feifan.control;

import com.alibaba.fastjson.JSON;
import com.feifan.service.IUserService;
import com.feifan.service.UserService;
import com.feifan.vo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(name = "UserControl", urlPatterns = "/person")
public class UserControl extends HttpServlet {
    private UserService service=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        switch (action) {
            case "show":
                show(resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "add":
               add(req, resp);
            case "update":
                update(req,resp);
                break;
            default:
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        String id=req.getParameter("id");
        PrintWriter out=null;
        User user=service.update(Integer.parseInt(id));//把从前端找到的对象从后端找到的结果找到的对象取到
        try {
            out=resp.getWriter();
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html");
            String json=JSON.toJSONString(user,true);
            out.write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.flush();//清空服务端数据直接发送到前端
        out.close();
    }

    public void add(HttpServletRequest req, HttpServletResponse resp){
        //往数据库里面传有两个方面 一个就是直接增加新的数据；还有一个就是把之前的旧数据修改了传进去
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String id=req.getParameter("id");//修改原来的数据要用的id
        User user=new User();
        if(!id.equals("")){//如果这个时候取到的不id为空的话，说明当前是修改旧的数据
            user.setId(Integer.parseInt(id));//把当前从前端取到的隐性数据id传到数据库去修改其用户名密码
        }
        user.setUsername(username);
        user.setPassword(password);
        boolean a=service.add(user);
        PrintWriter out=null;
        try {
            out=resp.getWriter();
            resp.setCharacterEncoding("utf-8");
            //后端往前端传可以有中文
            resp.setContentType("text/html");
            //后端往前端传类型为文本
            out.write(a+"");//从后端传到前端去
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.flush();//清空服务端数据直接发送到前端
        out.close();
    }
    public void show(HttpServletResponse resp) {
        PrintWriter out = null;
        try {
            resp.setCharacterEncoding("utf-8");
            //后端往前端传可以有中文
            resp.setContentType("text/html");
            //后端往前端传类型为文本
            out = resp.getWriter();
            //后端使用out进行数据传输
//            out.write("恭喜你成功了！");//要发送的数据
                List<User> list = service.show();
            //转为json格式 再用到多个属性的时候来用
//            System.out.println(json);//打印来看看
            out.write(JSON.toJSONString(list, true));
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.flush();//每次传完后记得清空服务端数据
        out.close();
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        PrintWriter out=null;
        boolean d = service.delete(Integer.parseInt(id));//把前端的id传到后端
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        try {
            out=resp.getWriter();
            out.write(d+"");//把真假穿到前端去
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.flush();//清空服务端数据直接发送到前端
        out.close();
    }
}
